package models

import "github.com/astaxie/beego/orm"

type NumberGroup struct{
	Id int64
	Group_name string
	Own_id int
	Create_time string
}



func (a *NumberGroup) TableName() string {
	return TableName("number_group")
}

func NumberGroupAdd(a *NumberGroup) (int64, error) {
	return orm.NewOrm().Insert(a)
}

func NumberGroupGetByName(group_name string,own_id int) (*NumberGroup, error) {
	a := new(NumberGroup)
	err := orm.NewOrm().QueryTable(TableName("number_group")).Filter("group_name",group_name).Filter("own_id",own_id).One(a)
	if err != nil {
		return nil, err
	}
	return a, nil
}

func NumberGroupGetList(page, pageSize int, filters ...interface{}) ([]*NumberGroup, int64) {
	offset := (page - 1) * pageSize
	list := make([]*NumberGroup, 0)
	query := orm.NewOrm().QueryTable(TableName("number_group"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("-id").Limit(pageSize, offset).All(&list)

	return list, total
}

func NumberGroupIdName() ([]*NumberGroup, error) {
	list := make([]*NumberGroup, 0)
	query := orm.NewOrm().QueryTable(TableName("number_group"))
	_,err:=query.OrderBy("-id").All(&list)
	if err!=nil{
		return nil,err
	}

	return list, nil
}

func NumberGroupIdName1(id int) ([]*NumberGroup, error) {
	list := make([]*NumberGroup, 0)
	query := orm.NewOrm().QueryTable(TableName("number_group")).Filter("own_id",id)
	_,err:=query.OrderBy("-id").All(&list)
	if err!=nil{
		return nil,err
	}

	return list, nil
}

func NumberGroupGetById(id int) (*NumberGroup, error) {
	r := new(NumberGroup)
	err := orm.NewOrm().QueryTable(TableName("number_group")).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

func (a *NumberGroup) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}


func (a *NumberGroup) Delete() error {
	if _, err := orm.NewOrm().Delete(a); err != nil {
		return err
	}
	return nil
}
